class TextUtils {
  // 判断字符串是否为空或仅包含空格
  static bool isEmpty(String? str) {
    return str == null || str.trim().isEmpty;
  }

  // 判断字符串是否非空
  static bool isNotEmpty(String? str) {
    return !isEmpty(str);
  }

  // 其他字符串处理方法
  static String? truncate(String? str, int maxLength) {
    if (isEmpty(str)) return null;
    return str!.length > maxLength ? str.substring(0, maxLength) : str;
  }

  static String? toUpperCase(String? str) {
    return str?.toUpperCase();
  }

  static Map<String, String> extractStorySections(String input) {
    final hasDetails = input.contains('<details');

    if (hasDetails) {
      final summaryReg = RegExp(r'<summary>(.*?)<\/summary>', dotAll: true);
      final detailsReg = RegExp(r'<details.*?>(.*?)<\/details>', dotAll: true);
      final afterDetailsReg = RegExp(r'<\/details>(.*)', dotAll: true);
      final hasDetailsClose = afterDetailsReg.hasMatch(input);
      if (!hasDetailsClose) {
        input+="<\/details>";
      }

      final summary = summaryReg.firstMatch(input)?.group(1)?.trim() ?? '';
      final detailsContent = detailsReg.firstMatch(input)?.group(1) ?? '';
      final thinking = detailsContent.replaceAll(summaryReg, '').trim();
      final story = afterDetailsReg.firstMatch(input)?.group(1)?.trim() ?? '';

      return {
        'summary': summary,
        'thinking': thinking,
        'story': story,
      };
    } else {
      return {
        'summary': '',
        'thinking': '',
        'story': input.trim(),
      };
    }
  }
}
